package qiniu.voidcepc.voicepc.demos.web.utils;

import com.alibaba.nls.client.AccessToken;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import com.alibaba.nls.client.protocol.NlsClient;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class Tokenutil {

    @Value("${aliyun.AccessKey}")
    private String id;//AccessKey

    @Value("${aliyun.AccessKeySecret}")
    private String appSecret;//AccessKeySecret

    @Value("${aliyun.url}")
    private String url;//url

    /**
     * NlsClient 为了避免重复加载所以我们采用本地指开发一次的方式进行开发
     * */
    @Bean
    public NlsClient getNlsClient() {
        AccessToken accessToken = new AccessToken(id, appSecret);
        try {
            accessToken.apply();
            System.out.println("get token: " + accessToken.getToken() + ", expire time: " + accessToken.getExpireTime());
            if(url.isEmpty()) {
                return  new NlsClient(accessToken.getToken());
            }else {
                return new NlsClient(url, accessToken.getToken());
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
